package com.example.demo.controller;

import com.example.demo.po.User;
import com.example.demo.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/users")
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/{id}")
    public User getById(@PathVariable Long id) {
        return userService.getById(id);
    }

    @GetMapping
    public List<User> getAll() {
        return userService.getAll();
    }

    @PostMapping
    public boolean create(@RequestBody User user) {
        return userService.create(user);
    }

    @PutMapping("/{id}")
    public boolean update(@PathVariable Long id, @RequestBody User user) {
        user.setId(id);
        return userService.update(user);
    }

    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Long id) {
        return userService.deleteById(id);
    }

    @PostMapping("/batch")
    public boolean batchCreate(@RequestBody java.util.List<User> users) {
        return userService.batchCreate(users);
    }

    @GetMapping("/page")
    public java.util.List<User> listPage(@RequestParam int pageNum, @RequestParam int pageSize) {
        return userService.listPage(pageNum, pageSize);
    }

    @GetMapping("/phone")
    public User getByPhone(@RequestParam String phone) {
        return userService.getByPhone(phone);
    }
}